package com.example.mydy.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.UserInfo;

import java.util.List;

public class CustomUserRewardAdapter extends RecyclerView.Adapter<CustomUserRewardAdapter.ViewHolder> {
    private List<UserInfo> users;
    private OnUserRemoveListener removeListener;

    public interface OnUserRemoveListener {
        void onUserRemove(UserInfo user);
    }

    public CustomUserRewardAdapter(List<UserInfo> users, OnUserRemoveListener listener) {
        this.users = users;
        this.removeListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_custom_user_reward, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        UserInfo user = users.get(position);
        holder.usernameText.setText(user.getUsername());
        holder.removeButton.setOnClickListener(v -> {
            if (removeListener != null) {
                removeListener.onUserRemove(user);
            }
        });
    }

    @Override
    public int getItemCount() {
        return users.size();
    }

    public void updateData(List<UserInfo> newUsers) {
        this.users = newUsers;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView usernameText;
        ImageButton removeButton;

        ViewHolder(View itemView) {
            super(itemView);
            usernameText = itemView.findViewById(R.id.usernameText);
            removeButton = itemView.findViewById(R.id.removeButton);
        }
    }
} 